import 'package:pp_uikit/pp_selection_entity.dart';

const double DESIGN_SELECTION_HEIGHT = 268;

typedef SingleListItemSelect = void Function(
    int listIndex, int index, PPSelectionEntity entity);

class PPSelectionUtil {
  /// 筛选项最多不超过三层,故直接写代码判断,本质为深度优先搜索。
  static int getTotalLevel(PPSelectionEntity entity) {
    int level = 0;
    PPSelectionEntity rootEntity = entity;
    while (rootEntity.parent != null) {
      rootEntity = rootEntity.parent!;
    }

    if (rootEntity.children.isNotEmpty) {
      level = level > 1 ? level : 1;
      for (PPSelectionEntity firstLevelEntity in rootEntity.children) {
        if (firstLevelEntity.children.isNotEmpty) {
          level = level > 2 ? level : 2;
          for (PPSelectionEntity secondLevelEntity
              in firstLevelEntity.children) {
            if (secondLevelEntity.children.isNotEmpty) {
              level = 3;
              break;
            }
          }
        }
      }
    }
    return level;
  }

  /// 确定当前 Item 在第几层级
  static int getCurrentListIndex(PPSelectionEntity? currentItem) {
    int listIndex = -1;
    if (currentItem != null) {
      listIndex = 0;
      var parent = currentItem.parent;
      while (parent != null) {
        listIndex++;
        parent = parent.parent;
      }
    }
    return listIndex;
  }
}
